package com.caigou.controller;

import com.caigou.bean.cms.entity.ContributeChannel;
import com.caigou.bean.cms.entity.ContributePublishType;
import com.caigou.bean.cms.entity.ContributeType;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.ContributeService;
import com.caigou.service.IClientsService;
import com.caigou.service.LessonService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/contribute/")
public class ContributeController {
    @Resource
    private ContributeService contributeService;
    @Resource
    private IClientsService clientsService;
    @Resource
    private LessonService lessonService;

    @RequestMapping("listContributeChannel")
    @PreAuthorize("hasAnyAuthority('chakantougao')")
    public Result listContributeChannel(){
        List<ContributeChannel> list = contributeService.listContributeChannel();
        return Result.success(list);
    }

    @RequestMapping("addContributeChannel")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result addContributeChannel(@RequestBody ContributeChannelParam param, @CurrentUser Users user){
        if(param.getChannelName()==null){
            return Result.error("投稿频道名称不能为空", "投稿频道名称不能为空");
        }
        if(param.getChannelName().length()>6){
            return Result.error("投稿频道名称不能超过6个字符", "投稿频道名称不能超过6个字符");
        }
        return contributeService.saveContributeChannel(param);
    }

    @RequestMapping("updateContributeChannel")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result updateContributeChannel(@RequestBody ContributeChannelParam param, @CurrentUser Users user){
        return contributeService.updateContributeChannel(param);
    }

    @RequestMapping("deleteContributeChannel")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result deleteContributeChannel(@RequestBody ContributeChannelParam param, @CurrentUser Users user){
        return contributeService.deleteContributeChannel(param.getId());
    }

    @RequestMapping("listContributeType")
    @PreAuthorize("hasAnyAuthority('chakantougao')")
    public Result listContributeType(@CurrentUser Users user){
        List<ContributeType> list = contributeService.listContributeType();
        return Result.success(list);
    }

    @RequestMapping("addContributeType")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result addContributeChannel(@RequestBody ContributeTypeParam param, @CurrentUser Users user){
        if(param.getTypeName()==null){
            return Result.error("投稿类型名称不能为空", "投稿类型名称不能为空");
        }
        if(param.getTypeName().length()>6){
            return Result.error("稿件类型名称不能超过6个字符", "稿件类型名称不能超过6个字符");
        }
        return contributeService.saveContributeType(param);
    }

    @RequestMapping("updateContributeType")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result updateContributeType(@RequestBody ContributeTypeParam param, @CurrentUser Users user){
        return contributeService.updateContributeType(param);
    }

    @RequestMapping("deleteContributeType")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result deleteContributeType(@RequestBody ContributeTypeParam param, @CurrentUser Users user){
        return contributeService.deleteContributeType(param.getId());
    }

    @RequestMapping("saveContributeConfig")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result saveContributeConfig(@RequestBody AttachmentConfigParam param, @CurrentUser Users user){
        return contributeService.saveContributeConfig(param);
    }

    @RequestMapping("getContributeConfig")
    @PreAuthorize("hasAnyAuthority('chakantougao')")
    public Result getContributeConfig(@CurrentUser Users user){
        return contributeService.getContributeConfig();
    }

    @RequestMapping("queryContribute")
    @PreAuthorize("hasAnyAuthority('chakantougao')")
    public Result queryContribute(@RequestBody ContributeQueryParam param, @CurrentUser Users user){
        if(param.getStartContributeTime()!=null){
            param.setStartContributeTime(param.getStartContributeTime()+" 00:00:00");
        }
        if(param.getEndContributeTime()!=null){
            param.setEndContributeTime(param.getEndContributeTime()+" 23:59:59");
        }
        if(param.getStartProcessTime()!=null){
            param.setStartProcessTime(param.getStartProcessTime()+" 00:00:00");
        }
        if(param.getEndProcessTime()!=null){
            param.setEndProcessTime(param.getEndProcessTime()+" 23:59:59");
        }
        return contributeService.queryContribute(param);
    }

    @RequestMapping("getContributeDetail")
    @PreAuthorize("hasAnyAuthority('chakantougao')")
    public Result getContributeDetail(@RequestBody ContributeParam param, @CurrentUser Users user){
        return contributeService.getContributeDetail(param);
    }

    @RequestMapping("processContribute")
    @PreAuthorize("hasAnyAuthority('tougaobianjichulijieguo')")
    public Result processContribute(@RequestBody ContributeParam param, @CurrentUser Users user){
        if(param.getProcessStatus().intValue()!=1&&param.getProcessStatus()!=2){
            return Result.error("请选择处理状态", "请选择处理状态");
        }
        if(param.getProcessNote()!=null&&param.getProcessNote().length()>150){
            return Result.error("处理方法备注不能超过150个字", "处理方法备注不能超过150个字");
        }
        return contributeService.processContribute(param, user.getUserid());
    }

    @RequestMapping("getUnProcessCounts")
    public Result getUnProcessCounts(@CurrentUser Users user){
        int unProcessContributeCount = contributeService.getCountByProcessStatus(0);
        Map resultMap = new HashMap<String, Object>();
        resultMap.put("unProcessContributeCount", unProcessContributeCount);

        int unHandleCooperationCount = clientsService.getCooperationCountByHandleStatus(0);
        resultMap.put("unHandleCooperationCount", unHandleCooperationCount);
        int unHandleReportCount = clientsService.getReportCountByHandleStatus(0);
        resultMap.put("unHandleReportCount", unHandleReportCount);
        int unHandleFeedbackCount = clientsService.getFeedbackCountByHandleStatus(0);
        resultMap.put("unHandleFeedbackCount", unHandleFeedbackCount);

        int unProcessLessonCount = lessonService.getCountByProcessStatus(0);
        resultMap.put("unProcessLessonCount", unProcessLessonCount);
        return Result.success(resultMap);
    }

    @RequestMapping("listContributePublishType")
    @PreAuthorize("hasAnyAuthority('chakantougao')")
    public Result listContributePublishType(@CurrentUser Users user){
        List<ContributePublishType> list = contributeService.listContributePublishType();
        return Result.success(list);
    }

    @RequestMapping("addContributePublishType")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result addContributePublishType(@RequestBody ContributePublishTypeParam param, @CurrentUser Users user){
        if(param.getPublishTypeName()==null){
            return Result.error("发布类型名称不能为空", "发布类型名称不能为空");
        }
        if(param.getPublishTypeName().length()>6){
            return Result.error("发布类型名称不能超过6个字符", "发布类型名称不能超过6个字符");
        }
        return contributeService.saveContributePublishType(param);
    }

    @RequestMapping("updateContributePublishType")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result updateContributePublishType(@RequestBody ContributePublishTypeParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }
        return contributeService.updateContributePublishType(param);
    }

    @RequestMapping("deleteContributePublishType")
    @PreAuthorize("hasAnyAuthority('tougaoshezhi')")
    public Result deleteContributePublishType(@RequestBody ContributePublishTypeParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }
        return contributeService.deleteContributePublishType(param.getId());
    }

}
